package game.module.hall.processor;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import game.HallServer;
import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageHall.C2SBindAgent;
import lua.LuaMessageHall.S2CBindAgent;
import sdk.common.HttpClientUtils;

@MsgCodeAnn(msgcode = C2SBindAgent.id, accessLimit = 500)
public class AgentBindProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(AgentBindProcessor.class);
	private static String agentServerHost = HallServer.getInstance().getServerConfig().getAgentServerUrl();
	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		
		C2SBindAgent reqMsg = C2SBindAgent.parse(request);
		String agentId = reqMsg.agent_id;
		int playerId = hero.getId();
		logger.info("agent bind,playeyId={},agentId = {}", playerId,agentId);
		HallServer.executorService.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> requestObj = new HashMap<>();
				requestObj.put("module", "bind_agent");
				requestObj.put("param", "do_bind");
				requestObj.put("playerId", playerId);
				requestObj.put("agentId", agentId);
				ObjectMapper objectMapper = new ObjectMapper();
				String requestParams;
				try {
					String agentServerUrl = agentServerHost;
					logger.info("query start");
					requestParams = objectMapper.writeValueAsString(requestObj);
					Map<String, Object> respObj = HttpClientUtils.getInstance().doHttpPost(agentServerUrl, requestParams);
					boolean ret = (Boolean) respObj.get("ret");
					logger.info("bind_agent ret={}", ret);
					
					if (ret==true)//绑定成功
					{
						String agentName = (String) respObj.get("agentName");
						//返回客户端
						S2CBindAgent respMsg = new S2CBindAgent(agentId, agentName, 0);//0表示成功
						hero.writeAndFlush(respMsg.build(hero.alloc()));
						return;
					}else //失败
					{
						//返回客户端
						S2CBindAgent respMsg = new S2CBindAgent(agentId,"", -1);//-1表示失败
						hero.writeAndFlush(respMsg.build(hero.alloc()));
						return;
					}
					
				} catch (JsonProcessingException e) {
					logger.error("", e);
				}
			}
		});
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// null
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// null
	}

	public static void main(String[] args) {
		Map<String, Object> requestObj = new HashMap<>();
		requestObj.put("module", "bind_agent");
		requestObj.put("param", "do_bind");
		requestObj.put("playerId", 169);
		requestObj.put("agentId", "agent");
		ObjectMapper objectMapper = new ObjectMapper();
		String requestParams;
		try {
			String agentServerUrl = "http://192.168.0.145:9090/GuanQuWeb/api/agent";
			logger.info("query start");
			requestParams = objectMapper.writeValueAsString(requestObj);
			long s1 = System.currentTimeMillis();
			Map<String, Object> respObj = HttpClientUtils.getInstance().doHttpPost(agentServerUrl, requestParams);
			logger.info("cost={}", System.currentTimeMillis() - s1);
			boolean ret = (Boolean) respObj.get("ret");
			logger.info("ret={}", ret);
		} catch (JsonProcessingException e) {
			logger.error("", e);
		}
		System.out.println(System.currentTimeMillis());
	}

}
